﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace VisualTest2.Drawings
{
    public class VisualHost : FrameworkElement
    {
        private VisualCollection children;
        private Pen pen = new Pen(Brushes.Red, 1);
        private Brush brush = Brushes.Aqua;
        public PointCollection collection = null;
        public VisualHost(PointCollection polis)
        {
            pen.Freeze();
            brush.Freeze();
            children = new VisualCollection(this);
            var visual = new DrawingVisual();
            children.Add(visual);
            StreamGeometry tempStream = createStreamGeometry(polis);
            using (var dc = visual.RenderOpen())
            {
                dc.DrawGeometry(Brushes.Aqua, pen, tempStream);
            }
        }
        protected StreamGeometry createStreamGeometry(PointCollection collection)
        {
            this.collection = collection;
            StreamGeometry geometry = new StreamGeometry();
            geometry.FillRule = FillRule.EvenOdd;
            using (StreamGeometryContext ctx = geometry.Open())
            {
                ctx.BeginFigure(new Point(collection[0].X, collection[collection.Count - 1].Y), false /* is filled */, false /* is closed */);
                ctx.PolyLineTo(collection, true, false);
            }
            geometry.Freeze();
            return geometry;

        }
        protected override int VisualChildrenCount
        {
            get { return children.Count; }
        }
        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= children.Count)
            {
                throw new ArgumentOutOfRangeException();
            }
            return children[index];
        }
    }
}
